"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 系统内部使用工具
Case Name   : 在root用户下执行pssh -H {正确主机名} {不存在源文件目录} {正确目标文件目录}
Create At   : 2022/4/24
@zou_jialiang0501129488
Description :
    1.在root用户下执行pssh -H {正确主机名} {不存在源文件目录} {正确目标文件目录}
    2.清理环境
Expect      :
    1.执行失败，报错code 1
    2.成功
History     :
"""
import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.Common import CommonSH
from testcase.utils.Logger import Logger

primary_sh = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 > primary_sh.get_node_num(), '非1+2不执行')
class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.primary_root_node = Node('PrimaryRoot')
        self.primary_node = Node('PrimaryDbUser')
        self.s_root_node1 = Node('Standby1Root')
        self.s_root_node1 = Node('Standby1Root')
        self.s_root_node2 = Node('Standby1Root')
        self.ssh_file = os.path.join('~', '.ssh', '*')
        self.s_node1 = Node("Standby1DbUser")
        self.s_node2 = Node("Standby2DbUser")
        self.com_root = CommonSH("PrimaryRoot")
        self.common = Common()
        self.dir_path = os.path.dirname(macro.DB_INSTANCE_PATH)

    def test_pscp(self):
        text = '-----step1:pssh -H {正确主机名} {不存在源文件目录} {正确目标文件目录};expect:失败-----'
        self.log.info(text)
        self.log.info("-----创建root互信-----")
        result = self.common.get_sh_result(self.primary_root_node,
                                           f"ls {macro.DB_SCRIPT_PATH}")
        if "gs_sshexkey" not in result:
            cmd = f"cd {macro.DB_SCRIPT_PATH}/../; " \
                  f"tar -zxvf openGauss-Package-bak*.tar.gz > /dev/null"
            result = self.common.get_sh_result(self.primary_root_node, cmd)
            self.log.info(result)
            result = self.common.get_sh_result(self.primary_root_node,
                                               f"ls {macro.DB_SCRIPT_PATH}")
            self.log.info(result)
            if "gs_sshexkey" not in result:
                raise Exception("cat not find gs_sshexkey, Please check!")

        self.host_tuple = (self.primary_root_node.ssh_host,
                           self.s_node1.ssh_host, self.s_node2.ssh_host)
        self.params = {'-f': 'test_hosts'}
        self.com_root.exec_gs_sshexkey(macro.DB_SCRIPT_PATH,
                                       *self.host_tuple,
                                       **self.params)
        test = '-----查看主机名-----'
        self.log.info(test)
        node_name = self.common.get_sh_result(self.s_root_node1, "uname -n")
        self.assertIsNotNone(node_name, '执行失败:' + text)
        file_path = os.path.join(f'{self.dir_path}', 'not_exists_file')
        pscp_cmd = f"pscp -H {node_name} {file_path} {self.dir_path}"
        pscp_res = self.common.get_sh_result(self.primary_root_node, pscp_cmd)
        self.assertIn('error code 1', pscp_res, '执行失败' + text)

    def tearDown(self):
        text = '-----step2:清理环境;expect:成功-----'
        self.log.info(text)
        self.log.info('-----删除root互信-----')
        del_cmd = f'rm -rf {self.ssh_file}'
        self.log.info(del_cmd)
        del_res1 = self.common.get_sh_result(self.primary_root_node, del_cmd)
        del_res2 = self.common.get_sh_result(self.s_root_node1, del_cmd)
        del_res3 = self.common.get_sh_result(self.s_root_node2, del_cmd)
        self.assertEqual('', del_res1, '互信删除失败')
        self.assertEqual('', del_res2, '互信删除失败')
        self.assertEqual('', del_res3, '互信删除失败')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
